#!/usr/bin/perl
#
# (C) Copyright 2012- Hewlett-Packard Development Company, L.P.
#
#  $Id: instance_provisioning.pl 26760 2013-03-13 08:34:39Z cheng-xin.cai@hp.com $
#
# This script acts as a wrapper that will call all involved scripts to perform each step required
# to complete the entire instance provisioning procedure
#

package main;

use strict;
use 5.008000;
use warnings;

use POSIX;
use Getopt::Long;
use Data::Dumper qw/Dumper/;
use English;

use lib "/opt/hp/nv/lib/perl";
use config ':all';

use Provision::Utils;
use Provision::Cluster;

# values particular to this script
my $myscriptname = "instance_provisioning";
my $revision = '$Revision: 26760 $';
$revision =~ s/\$//g;

my $script_loc = $nvpath . '/instance';

# options
my %gv_opts = ( 
    log  => "$script_loc/instance_log",
);


# Start new code CR 5620
sub checkLocalDisk {
    my $LocalDiskCheck = "/opt/hp/platform/validation/storage/LocalDiskCheck";
    # running LocalDiskCheck from platform
    if (-e $LocalDiskCheck) {
        my $out = `$LocalDiskCheck`;
        my $str = "$LocalDiskCheck was ";
        if (!($out =~ /SUCCESS/)) {
            $str .= "not successful";

            logMsg( $out . "\n" . $str );
            print "$out\n$str\n";

            if (!defined($gv_opts{noprompt})) {
                print "\tDo you wish to continue (y/n): ";
                my $ans = readline(*STDIN);
                print "\n";
                exit (config::UNKNOWN) if ($ans =~ /^n$/i);
            }
        }
        else {
            $str .= "successful: $out";
            logMsg( $str );
            print "$str\n";
        }
    }
    else {
        my $str = "$LocalDiskCheck does not exist, continuing";
        logMsg( $str );
        print "$str\n";
    }
}
# End new code CR 5620

# CHANGE FOR YOUR NEEDS
#
# This sub checks for any requirements that must be met before
# the configuration is performed.
#
sub pre_req_check {
    if ($EUID != 0) {
        logErrorDie("You must run this script as the root user id");
    }

    my $statuspath = "$script_loc/status";
    if( !-e $statuspath ){
        mkdir $statuspath;
        chmod 0777, $statuspath;
    }

    unless(-d $statuspath and -x $statuspath){
        logErrorDie("Unable to access directory: $statuspath");
    }

    return config::OK;
}

# CHANGE FOR YOUR NEEDS
#
# This sub verifies that the work done is complete and in such a state
# that it is ready for the next step to be done
#
sub post_check {
    return config::OK;
}

# CHANGE FOR YOUR NEEDS
#
# This sub verifies that the work done is complete and in such a state
# that it is ready for the next step to be done
#
sub perform_config {
    STDOUT->autoflush(1);

    checkLocalDisk();

    if ($gv_opts{debug}) {
        $ENV{INST_PROV_DEBUG} = 1;
    }

    my $cluster = Provision::Cluster->new();

    my $ret;
    if ( $gv_opts{add} ) { #add user
        if ( $gv_opts{squser}) {
            $ret = $cluster->add_user($gv_opts{squser}, $gv_opts{test});
        }
        else {
            logErrorDie('Must specify the squser to be added');
        }
    }
    else { #provision whole cluster
        $ret = $cluster->create($gv_opts{test});
    }

    if ($ret == 0) {
        #we need to copy all the config file to backup headnode
        $ret = performStep($cluster, { title => 'Synchronize head node',
                                       cmd => $script_loc . '/sync_headnodes',
                                     }, 
                           $gv_opts{test});
    }

    return $ret;
}


#
# usage
#
sub usage
{
print <<"EOF";

Usage: $0 [-h|--help] [-l|--log <log_file>]
          [--add < --squser=<squser> >]
          [--test] [--debug] [--nopropmpt]

Options:
	-h|--help:  Print this help information.
        -l|--log:   Specify the log file you want to use, default is
                    /opt/hp/nv/instance/instance_log
        --add:      Add new user to an existing shared privision instance
        --squser:   Specify the user name to be added
        -t|--test:  Run as the test mode
        -d|--debug: Run in a step by step mode
        --noprompt: Continue script without prompting
EOF

exit(config::UNKNOWN);
}

### Main ###
#
# This script starts here
#

&GetOptions( \%gv_opts,
             "log=s",
             "add", "squser=s", 
             "noprompt",
             "debug",
             "test",
             "help|?" => \&usage,
            ) || usage();

logInit( { file => $gv_opts{log} } );
 
logMsg("Starting script: revision number $revision");

pre_req_check;

perform_config;

post_check;

logMsg("Script completed successfully");

# Return success
exit(config::OK);

END {
    print "\nFor more information, please see $gv_opts{log}.\n";
}
